home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V2 / MDEF MacTech Column / Tester.c < prev    next >
C/C++ Source or Header  |  1994-05-03  |  6KB  |  320 lines

  1. /********************************************************/
  2. /*                                                        */
  3. /*    MDEF Tester Code from Chapter Three of                */
  4. /*                                                        */
  5. /*    The Macintosh Programming Primer, Volume II,        */
  6. /*                    Second Edition                        */
  7. /*                                                        */
  8. /*    Copyright 1993, Dave Mark                            */
  9. /*                                                        */
  10. /*    This program demonstrates specific Mac programming    */
  11. /*    techniques.                                            */
  12. /*                                                        */
  13. /********************************************************/
  14.  
  15. #define kWindowResID        128
  16. #define kMBARResID            128
  17. #define kNULLStorage        0L
  18. #define kMoveToFront        (WindowPtr)-1L
  19. #define kSleep                60L
  20.  
  21. #define mApple                128
  22. #define iAbout                1
  23.  
  24. #define mFile                129
  25. #define iQuit                1
  26.  
  27. #define mPICT                131
  28.  
  29.  
  30. /*************/
  31. /*  Globals  */
  32. /*************/
  33.  
  34. Boolean        gDone;
  35. short        gCurPICTid;
  36.  
  37.  
  38. /***************/
  39. /*  Functions  */
  40. /***************/
  41.  
  42. void    ToolboxInit( void );
  43. void    MenuBarInit( void );
  44. void    WindowInit( void );
  45. void    EventLoop( void );
  46. void    DoEvent( EventRecord *eventPtr );
  47. void    HandleMouseDown( EventRecord *eventPtr );
  48. void    HandleMenuChoice( long menuChoice );
  49. void    HandleAppleChoice( short item );
  50. void    HandleFileChoice( short item );
  51. void    HandlePICTChoice( short item );
  52. void    DoUpdate( WindowPtr window );
  53. void    DrawPictInWindow( PicHandle pic,
  54.                         WindowPtr window );
  55. short    GetBasePICTid( short menuID );
  56.  
  57.  
  58. /*********************************** main */
  59.  
  60. void    main( void )
  61. {
  62.     ToolboxInit();
  63.     MenuBarInit();
  64.     WindowInit();
  65.     
  66.     gCurPICTid = GetBasePICTid( mPICT );
  67.     
  68.     EventLoop();
  69. }
  70.  
  71.  
  72. /*********************************** ToolboxInit */
  73.  
  74. void    ToolboxInit( void )
  75. {
  76.     InitGraf( &thePort );
  77.     InitFonts();
  78.     InitWindows();
  79.     InitMenus();
  80.     TEInit();
  81.     InitDialogs( 0L );
  82.     InitCursor();
  83. }
  84.  
  85.  
  86. /***********************************    MenuBarInit    */
  87.  
  88. void    MenuBarInit( void )
  89. {
  90.     Handle            menuBar;
  91.     MenuHandle        menu;
  92.     
  93.     menuBar = GetNewMBar( kMBARResID );
  94.     SetMenuBar( menuBar );
  95.  
  96.     menu = GetMHandle( mApple );
  97.     AddResMenu( menu, 'DRVR' );
  98.     
  99.     DrawMenuBar();
  100. }
  101.  
  102.  
  103. /*********************************** WindowInit */
  104.  
  105. void    WindowInit( void )
  106. {
  107.     WindowPtr    window;
  108.     
  109.     window = GetNewWindow( kWindowResID, kNULLStorage, kMoveToFront );
  110.     
  111.     if ( window == NULL )
  112.     {
  113.         SysBeep( 20 );    /* Couldn't load WIND */
  114.         ExitToShell();
  115.     }
  116.     
  117.     SetPort( window );
  118.     ShowWindow( window );
  119. }
  120.  
  121.  
  122. /*********************************** EventLoop */
  123.  
  124. void    EventLoop( void )
  125. {        
  126.     EventRecord        event;
  127.     
  128.     gDone = false;
  129.     while ( gDone == false )
  130.     {
  131.         if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
  132.             DoEvent( &event );
  133.     }
  134. }
  135.  
  136.  
  137. /************************************* DoEvent     */
  138.  
  139. void    DoEvent( EventRecord *eventPtr )
  140. {
  141.     char    theChar;
  142.     
  143.     switch ( eventPtr->what )
  144.     {
  145.         case mouseDown: 
  146.             HandleMouseDown( eventPtr );
  147.             break;
  148.         case keyDown:
  149.         case autoKey:
  150.             theChar = eventPtr->message & charCodeMask;
  151.             if ( (eventPtr->modifiers & cmdKey) != 0 ) 
  152.                 HandleMenuChoice( MenuKey( theChar ) );
  153.             break;
  154.         case updateEvt:
  155.             DoUpdate( (WindowPtr)eventPtr->message );
  156.             break;
  157.     }
  158. }
  159.  
  160.  
  161. /************************************* HandleMouseDown */
  162.  
  163. void    HandleMouseDown( EventRecord *eventPtr )
  164. {
  165.     WindowPtr        window;
  166.     short            thePart;
  167.     long            menuChoice;
  168.     
  169.     thePart = FindWindow( eventPtr->where, &window );
  170.     
  171.     switch ( thePart )
  172.     {
  173.         case inMenuBar:
  174.             menuChoice = MenuSelect( eventPtr->where );
  175.             HandleMenuChoice( menuChoice );
  176.             break;
  177.         case inSysWindow: 
  178.             SystemClick( eventPtr, window );
  179.             break;
  180.         case inDrag : 
  181.             DragWindow( window, eventPtr->where, &(screenBits.bounds) );
  182.             break;
  183.     }
  184. }
  185.  
  186.  
  187. /************************************* HandleMenuChoice */
  188.  
  189. void    HandleMenuChoice( long menuChoice )
  190. {
  191.     short    menu;
  192.     short    item;
  193.     
  194.     if ( menuChoice != 0 )
  195.     {
  196.         menu = HiWord( menuChoice );
  197.         item = LoWord( menuChoice );
  198.         
  199.         switch ( menu )
  200.         {
  201.             case mApple:
  202.                 HandleAppleChoice( item );
  203.                 break;
  204.             case mFile:
  205.                 HandleFileChoice( item );
  206.                 break;
  207.             case mPICT:
  208.                 HandlePICTChoice( item );
  209.                 break;
  210.         }
  211.         HiliteMenu( 0 );
  212.     }
  213. }
  214.  
  215.  
  216. /************************************* HandleAppleChoice */
  217.  
  218. void    HandleAppleChoice( short item )
  219. {
  220.     MenuHandle    appleMenu;
  221.     Str255        accName;
  222.     short        accNumber;
  223.     
  224.     switch ( item )
  225.     {
  226.         case iAbout:
  227.             SysBeep( 20 );
  228.             break;
  229.         default:
  230.             appleMenu = GetMHandle( mApple );
  231.             GetItem( appleMenu, item, accName );
  232.             accNumber = OpenDeskAcc( accName );
  233.             break;
  234.     }
  235. }
  236.  
  237.  
  238. /************************************* HandleFileChoice */
  239.  
  240. void    HandleFileChoice( short item )
  241. {
  242.     switch ( item )
  243.     {
  244.         case iQuit :
  245.             gDone = true;
  246.             break;
  247.     }
  248. }
  249.  
  250.  
  251. /************************************* HandlePICTChoice */
  252.  
  253. void    HandlePICTChoice( short item )
  254. {
  255.     WindowPtr    window;
  256.     
  257.     window = FrontWindow();
  258.     
  259.     EraseRect( &window->portRect );
  260.     InvalRect( &window->portRect );
  261.     
  262.     gCurPICTid = GetBasePICTid( mPICT ) + item - 1;
  263. }
  264.  
  265.  
  266. /************************************* DoUpdate */
  267.  
  268. void    DoUpdate( WindowPtr window )
  269. {
  270.     PicHandle    pic;
  271.     
  272.     BeginUpdate( window );
  273.     
  274.     pic = GetPicture( gCurPICTid );
  275.  
  276.     if ( pic == NULL )
  277.     {
  278.         SysBeep( 20 );    /* Couldn't load PICT */
  279.         ExitToShell();
  280.     }
  281.     
  282.     DrawPictInWindow( pic, FrontWindow() );
  283.     
  284.     EndUpdate( window );
  285. }
  286.  
  287.  
  288. /*********************************** DrawPictInWindow */
  289.  
  290. void    DrawPictInWindow( PicHandle pic,
  291.                             WindowPtr window )
  292. {
  293.     Rect        pictRect, windRect;
  294.     
  295.     pictRect = (**pic).picFrame;
  296.     
  297.     windRect = window->portRect;
  298.     
  299.     OffsetRect( &pictRect, windRect.left - pictRect.left,
  300.                            windRect.top     - pictRect.top);
  301.     OffsetRect( &pictRect,(windRect.right - pictRect.right)/2,
  302.                           (windRect.bottom - pictRect.bottom)/2);
  303.     
  304.     DrawPicture( pic, &pictRect );
  305. }
  306.  
  307.  
  308. /*********************************** GetBasePICTid */
  309.  
  310. short    GetBasePICTid( short menuID )
  311. {
  312.     Handle    longHandle;
  313.     long    retrievedLong;
  314.     
  315.     longHandle = GetResource( 'long', menuID );
  316.     
  317.     retrievedLong = (*((long *)(*longHandle)));
  318.     
  319.     return( HiWord( retrievedLong ) );
  320. }